﻿using System.Windows.Data;
namespace DeepEarth.Toolkit.Controls
{
    public class ZeroOrEmptyStringConverter : IValueConverter
    {
        static object Convert(object value, System.Type targetType)
        {
            if (targetType == typeof(double))
            {
                double parsed;
                if (double.TryParse(value.ToString(), out parsed))
                    return parsed > 0 ? parsed : 1;
                return 1;
            }
            else
            {
                return value.ToString();
            }
        }


        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Convert(value, targetType);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Convert(value, targetType);
        }
    }
}
